package ac.github.os.internal.core.action.plugin

import ac.github.os.internal.core.action.IEntityAction
import ac.github.os.internal.core.config.StringConfig
import ac.github.os.internal.core.config.get
import ac.github.os.internal.core.objective.EntityObjective
import ac.github.os.internal.session.Cache
import ac.github.os.internal.session.Session
import org.bukkit.entity.Player
import taboolib.common.platform.Awake
import taboolib.common.platform.function.adaptPlayer
import taboolib.library.kether.LocalizedException

import taboolib.module.kether.KetherShell
import taboolib.module.kether.printKetherErrorMessage

@Awake
class KetherAction : IEntityAction {

    override fun onExecute(session: Session, cache: Cache, config: StringConfig) {
        val s = config.get<String>("value", single = true) ?: return
        cache.objectives.filterIsInstance<EntityObjective>().forEach {
            if (it.entity is Player) {

                try {
                    KetherShell.eval(s, sender = adaptPlayer(it.entity))
                } catch (e: LocalizedException) {
                    e.printKetherErrorMessage()
                }
            }
        }
    }

    override val namespace: String
        get() = "kether"
}